package com.zsd.android.common

import android.app.Activity
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.view.Gravity
import android.view.View
import android.view.animation.Animation
import android.widget.LinearLayout
import android.widget.PopupWindow

abstract class BasePopupWindow(mContext: Activity, width: Int, height: Int) {

    protected var mContext: Activity
    protected var mPopupWindow: PopupWindow
    protected var mPopupView: View
    protected var mAnimView: View?
    protected var mShowAnimation: Animation?
    protected var mDisAnimation: Animation?
    protected var isShowing: Boolean

    constructor(mContext: Activity) : this(mContext, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT)

    init {
        this.mContext = mContext
        isShowing = false
        mPopupView = getPopupView()
        mAnimView = getAnimView()
        mShowAnimation = getShowAnimation()
        mDisAnimation = getDisAnimation()

        mPopupWindow = PopupWindow(mPopupView, width, height)
        mPopupWindow.isOutsideTouchable = true
        mPopupWindow.isFocusable = true
        mPopupWindow.animationStyle = 0
        mPopupWindow.setBackgroundDrawable(ColorDrawable())

        initView()
    }

    protected open fun initView() {}

    fun showPopupWindow() {
        try2ShowPopup(0, null)
    }

    fun showPopupWindow(res: Int) {
        try2ShowPopup(res, null)
    }

    fun showPopupWindow(v: View) {
        try2ShowPopup(0, v)
    }

    open fun try2ShowPopup(res: Int, v: View?) {
        //传递了view
        if (res == 0 && v != null) {
            var location = IntArray(2)
            v.getLocationOnScreen(location)
            mPopupWindow.showAtLocation(v, Gravity.NO_GRAVITY, location[0] - 8, location[1] + v.height)
        }
        //传递了res
        if (res != 0 && v == null) {
            mPopupWindow.showAtLocation(mContext.findViewById(res), Gravity.CENTER, 0, 0)
        }
        //什么都没传递，取顶级view的id
        if (res == 0 && v == null) {
            mPopupWindow.showAtLocation(
                mContext.findViewById(android.R.id.content),
                Gravity.CENTER,
                0,
                0
            )
        }
        if (mShowAnimation != null && mAnimView != null) {
            mAnimView?.clearAnimation()
            mAnimView?.startAnimation(mShowAnimation)
        }
        isShowing = true
    }

    /**
     * 设置PopupWindow是否覆盖状态栏
     *
     * @param needFullScreen true覆盖状态栏，false不覆盖
     */
    fun fitOverSystemBar(needFullScreen: Boolean) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            var mLayoutInScreen = PopupWindow::class.java.getDeclaredField("mLayoutInScreen")
            mLayoutInScreen.isAccessible = true
            mLayoutInScreen.set(mPopupWindow, needFullScreen)
        }
    }

    abstract fun getPopupView() : View

    abstract fun getAnimView(): View?

    abstract fun getShowAnimation() : Animation?

    abstract fun getDisAnimation() : Animation?
}